package com.yy.young.business.web;

import com.yy.young.common.util.CommonUtil;
import com.yy.young.common.util.Result;
import com.yy.young.common.util.StringUtils;
import com.yy.young.dal.util.Page;
import com.yy.young.interfaces.log.annotation.Log;
import com.yy.young.business.model.Project;
import com.yy.young.business.service.IProjectService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 项目服务
 * Created by rookie on 2018-03-09.
 */
@Controller
@RequestMapping("/business/project")
public class ProjectController {

    @Resource(name="projectService")
    IProjectService service;

    @Log("项目登记")
    @RequestMapping("/register")
    public Object register(HttpServletRequest request) throws Exception{
        return "/page/business/projectRegister.jsp";
    }

    /**
     * 获取数据列表
     * @param project
     * @param request
     * @return
     * @throws Exception
     */
    @Log("查询项目列表")
    @RequestMapping("/getProjectList")
    @ResponseBody
    public Object getProjectList(Project project, HttpServletRequest request) throws Exception{
        List<Project> list = service.getProjectList(project);
        return new Result(list);
    }

    /**
     * 获取分页数据
     * @param project
     * @param request
     * @return
     * @throws Exception
     */
    @Log("分页查询项目列表")
    @RequestMapping("/getProjectPage")
    @ResponseBody
    public Object getProjectPage(Project project, HttpServletRequest request, HttpServletResponse response) throws Exception{
        //response.setHeader("Access-Control-Allow-Origin", "*");
        Page page = CommonUtil.getPageFromRequest(request);
        List<Project> list = service.getProjectPage(project, page);
        page.setData(list);
        return page;
    }

    /**
     * 获取单条数据
     * @param id
     * @param request
     * @return
     * @throws Exception
     */
    @Log("查询项目")
    @RequestMapping("/getProject")
    @ResponseBody
    public Object getProject(String id, HttpServletRequest request) throws Exception{
        Project project = service.getProject(id);
        return new Result(project);
    }

    /**
     * 新增
     * @param project
     * @param request
     * @return
     * @throws Exception
     */
    @Log("新增项目")
    @RequestMapping("/insert")
    @ResponseBody
    public Object insert(Project project, HttpServletRequest request) throws Exception{
        int num = service.insertProject(project);
        return new Result();
    }

    /**
     * 修改
     * @param project
     * @param request
     * @return
     * @throws Exception
     */
    @Log("修改项目")
    @RequestMapping("/update")
    @ResponseBody
    public Object update(Project project, HttpServletRequest request) throws Exception{
        int num = service.updateProject(project);
        return new Result();
    }

    /**
     * 删除
     * @param ids
     * @param id
     * @param request
     * @return
     * @throws Exception
     */
    @Log("删除项目")
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String ids, String id, HttpServletRequest request) throws Exception{
        Result result = new Result();
        if(StringUtils.isNotBlank(ids)) {
            String[] idArr = ids.split(",");
            int num = service.deleteProject(idArr);
        }else if(StringUtils.isNotBlank(id)){
            int num = service.deleteProject(id);
        }else{
            result.setCode(-1);
            result.setInfo("删除失败:待删除编号无效!");
        }
        return result;
    }

}